import com.R1DTO;
import com.R2DTO;
import com.RegisterDTO;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = {"/register"})
public class RegisterServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws IOException {
        //从jsp中获得注册数据，完成登录流程
        //获取表单post方式传送的数据
        String username=request.getAttribute("username").toString();
        String email=request.getAttribute("email").toString();
        String password=request.getAttribute("password").toString();

        //得到文件后
        System.out.println("这里是正常登录的servlet");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        //从jsp中获得注册数据，完成登录流程
        //获取表单post方式传送的数据
        String username=request.getParameter("username");
        String password=request.getParameter("password");

        //寻找用户是否注册
        String str;
        boolean isreg=false;
        boolean ispwd=false;
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream("Userpwd.txt")));
        while(bufferedReader.ready()){
            str=bufferedReader.readLine();
            String[] parts=str.split(",");
            if(parts.length==3&&parts[0].equals(username)){
                isreg=true;
                //用户存在，验证密码是否正确
                if(parts[1].equals(password))
                    ispwd=true;
            }
        }

        if(!isreg){
            response.getWriter().println("<script type=\"text/javascript\">\n" +
                    "        alert('用户未注册')\n" +
                    "        window.history.back();\n" +
                    "         \n" +
                    "</script>");
        }
        else if(!isreg&&!ispwd){
            response.getWriter().println("<script type=\"text/javascript\">\n" +
                    "        alert('密码错误')\n" +
                    "        window.history.back();\n" +
                    "         \n" +
                    "</script>");
        }
        else
        {
            request.getSession().setAttribute("islogin","true");
            response.sendRedirect("https://cas.dgut.edu.cn/logout?callback=http://localhost:8080/PersonInfo.jsp");
        }
    }
}